home *** CD-ROM | disk | FTP | other *** search
/ Giga Games 1 / Giga Games.iso / net / d_d / caltech / inbound / tg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-22  |  14.3 KB  |  475 lines

  1. /*
  2.    An Treasure genrator , usable with X11, it is written with Athena Widgets
  3.  It use Form, Label, and Command Widget.
  4.  
  5.    You need to do something like this for running this program :
  6.   
  7.    cc -I/local/X11R5/include -L/local/X11R5/lib -o tg tg.c -lXaw -lXmu -lXt -lXext -lX11
  8.  
  9.    
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <time.h>
  16.  
  17. #include <X11/Intrinsic.h>
  18. #include <X11/StringDefs.h>
  19.  
  20. #include <X11/Xaw/Cardinals.h>
  21.  
  22. /* Simple widgets                                             */
  23. #include <X11/Xaw/Command.h>        /* commandWidgetClass     */ 
  24. #include <X11/Xaw/Label.h>          /* labelWidgetClass       */ 
  25. #include <X11/Xaw/Logo.h>           /* logoWidgetClass        */ 
  26.  
  27. /* Composite and Constraint Widgets                           */
  28. #include <X11/Xaw/Form.h>           /* formWidgetClass        */ 
  29.  
  30. #include <X11/Shell.h>
  31.  
  32. static void quit(), Treasure(), PopDown(), Treasure_Gen();
  33. /* static void T_En(), Tre();  */
  34.  
  35. String fallback_resources[] = {
  36.  
  37.  "*borderWidth:           1",
  38.  "*defaultDistance:       4",
  39.  "*Treasure*Translations :#override \\n\
  40.     <KeyPress>: PopDown()",
  41.  "*Translations :#override \\n\
  42.     Ctrl<Key>C: quit()",
  43.  NULL
  44. };
  45.  
  46. XtActionsRec actions[] = {
  47.   { "quit",       (XtActionProc) quit },
  48.   { "PopDown",    (XtActionProc) PopDown},
  49. };
  50.  
  51. /************** Globale variabler ***************/
  52. Widget       root;            /* Root of all the widgets */
  53.  
  54.  
  55. main( argc, argv )
  56. int  argc;
  57. char **argv;
  58. {
  59.   XtAppContext app_con;
  60.   Widget       fo,cm,cm2,cm3;
  61.   int          i;
  62.   char         str[5],*s;
  63.  
  64.   argv[0] = "Treasure generator";
  65.   srand( (short) (time(NULL) % 37) );
  66.   
  67.   root = XtAppInitialize( &app_con, "Root",
  68.                           NULL, ZERO,         /* Option's parsering */
  69.               &argc, argv,
  70.                           fallback_resources, 
  71.               NULL, ZERO          /* Application shell parameter */
  72.                         );
  73.   
  74.   if ( argc != 1 )    /* Something has fucked up */
  75.     exit(1);
  76.  
  77.   XtAppAddActions( app_con, actions, XtNumber(actions) );
  78.  
  79.   fo = XtCreateManagedWidget( "Xform", formWidgetClass, root, NULL, ZERO );
  80.  
  81.   /**** Lager en vertikal rekke  ***********/
  82.   cm = XtVaCreateManagedWidget( "A", commandWidgetClass, fo, 
  83.                                 XtNwidth, 25, 
  84.                                 NULL );
  85.   XtAddCallback( cm, XtNcallback, Treasure_Gen, (XtPointer)NULL );
  86.   cm2 = cm;
  87.   str[1] = '\0';
  88.   for( i = 66; i < 76; i++ ) {
  89.     str[0] = i; 
  90.     cm = XtVaCreateManagedWidget( str, commandWidgetClass, fo,
  91.                                   XtNfromVert, cm, 
  92.                                   XtNwidth, 25, 
  93.                                   NULL );
  94.     XtAddCallback( cm, XtNcallback, Treasure_Gen, (XtPointer)NULL );
  95.   }
  96.  
  97.   /***** Lager en horisontal rekke *********/
  98.   cm3 = cm2;
  99.   for( i = 76; i < 87; i++ ) {
  100.     str[0] = i; 
  101.     cm3 = XtVaCreateManagedWidget( str, commandWidgetClass, fo,
  102.                                   XtNfromHoriz, cm3,
  103.                                   XtNwidth, 25, 
  104.                                   NULL );
  105.     XtAddCallback( cm3, XtNcallback, Treasure_Gen, (XtPointer)NULL );
  106.   }
  107.  
  108.   s = "\nWhich treasure (push a button) ?\n\n\
  109. The result is written to a file\n\
  110. named Treasure_(A..V)\n\n\
  111. Ctrl-C to quit the program."; 
  112.   cm = XtVaCreateManagedWidget("xl", labelWidgetClass,fo,
  113.                                XtNfromVert, cm3,
  114.                                XtNhorizDistance, 50,
  115.                                XtNvertDistance, 15,
  116.                                XtNborderWidth, 2,
  117.                                XtNlabel, s,
  118.                                NULL);
  119.  
  120.   cm = XtVaCreateManagedWidget("xlogo", logoWidgetClass,fo,
  121.                                XtNfromVert, cm3,
  122.                                XtNhorizDistance, 150,
  123.                                XtNvertDistance, 150,
  124.                                XtNborderWidth, 1,
  125.                                NULL );  
  126.   XtRealizeWidget( root );
  127.  
  128.   XtAppMainLoop( app_con );    /* Run until killed */  
  129. }                   
  130.  
  131. /***********************************************************************/
  132. /*
  133.  
  134. */
  135. static void Treasure( txt )
  136. char *txt;
  137. {
  138.  
  139.   Widget   fo,popup,cm,cm2,cm3;
  140.   Arg      args[5];
  141.   int      i;
  142.   Cardinal n;
  143.   Position width,height,x,y;
  144.   char     str[5],*s;
  145.  
  146.   n = 0;
  147.   XtSetArg(args[0], XtNwidth, &width); n++;
  148.   XtSetArg(args[1], XtNheight, &height); n++;
  149.   XtGetValues(root, args, n);
  150.   XtTranslateCoords(root, (Position) (width), (Position) (height/1024),
  151.                       &x, &y);
  152.   n = 0;
  153.   XtSetArg(args[n], XtNx, x);        n++;
  154.   XtSetArg(args[n], XtNy, y);        n++;
  155.   popup = XtCreatePopupShell("Treasure", transientShellWidgetClass, root,
  156.           args, n);
  157.  
  158.   /*fo = XtCreateManagedWidget( "form", formWidgetClass, popup, NULL, ZERO );*/
  159.   XtVaCreateManagedWidget( "xtxt", labelWidgetClass, popup, 
  160.                            XtNlabel, txt , NULL );
  161.  
  162.   XtPopup( popup, XtGrabExclusive );
  163. }
  164.  
  165. /***********************************************************************/
  166. /*
  167.     This is the callback rountine, which is called when we have pressed
  168.   A..V button on the screen. 
  169. */
  170. static void Treasure_Gen( w, client_data, call_data )
  171. Widget    w;
  172. XtPointer client_data,call_data;
  173. {
  174.  char *t_name,fname[60];
  175.  FILE *f;
  176.  
  177.  /** Make a file name for where we are going to write treasure stat **/ 
  178.  t_name   = XtName( w ); 
  179.  strcpy( fname, "Treasure_X" );
  180.  fname[9] = t_name[0];
  181.  
  182.  unlink( fname );
  183.  f = fopen( fname, "a+" );
  184.  Tre( t_name, f );
  185.  fclose( f ); 
  186. }
  187.  
  188.  
  189. /***********************************************************************/
  190. /*
  191.     T_En Generates the treasure, write it to a file, and pop a window
  192.   with the result.
  193. */
  194. int T_En(  PrCP, antCP, ternCP, PrSP, antSP, ternSP,
  195.            PrEP, antEP, ternEP, PrGP, antGP, ternGP,
  196.            PrPP, antPP, ternPP,
  197.            Prgem, antTG, ternG,
  198.            PrJew, antTJ, ternJ,
  199.            PrSpe, antTS, ternS,
  200.            PrMag, strM,
  201.            multi,f, id
  202.           )
  203. int  PrCP, antCP, ternCP,     /* Copper coins                 */
  204.      PrSP, antSP, ternSP,     /* Silver coins                 */
  205.      PrEP, antEP, ternEP,     /* Electium coins               */
  206.      PrGP, antGP, ternGP,     /* Gold coins                   */
  207.      PrPP, antPP, ternPP,     /* Platina coins                */
  208.      Prgem, antTG, ternG,     /* Gems                         */
  209.      PrJew, antTJ, ternJ,     /* Jewelry                      */
  210.      PrSpe, antTS, ternS,     /* Special Magical Treasure     */
  211.      PrMag;                   /* Magical Item                 */
  212. char *strM;
  213. int  multi;
  214. FILE *f;                      /* File to place the output     */
  215. char id;                      /* Name of treasure to generate */
  216.  
  217. {
  218.   unsigned int cp, sp, ep, gp, pp, gem, jew, spe, mag;
  219.   int          tall,i;
  220.   float        xp;
  221.   char         str[1024],txt[1024];
  222.  
  223.   cp = sp = ep = gp = pp = gem = jew = spe = mag = 0;
  224.  
  225.   if ( PrCP != 0 ) {
  226.     tall = rand() % 100;
  227.     if ( tall < PrCP ) {
  228.       for( i=1;  i <= antCP; i++ )
  229.         cp += ( rand() % ternCP );
  230.     }
  231.     cp *= multi;
  232.   }
  233.  
  234.   if ( PrSP != 0 ) {
  235.     tall = rand() % 100;
  236.     if ( tall < PrSP ) {
  237.       for( i=1;  i <= antSP; i++ )
  238.         sp += ( rand() % ternSP );
  239.     }
  240.     sp *= multi;
  241.   }
  242.  
  243.   if ( PrEP != 0 ) {
  244.     tall = rand() % 100;
  245.     if ( tall < PrEP ) {
  246.       for( i=1;  i <= antEP; i++ )
  247.         ep += ( rand() % ternEP );
  248.     }
  249.     if ( (id == 'H') )  ep *= (multi * 10);
  250.     else                ep *= multi;
  251.   }
  252.  
  253.   if ( PrGP != 0 ) {
  254.     tall = rand() % 100;
  255.     if ( tall < PrGP ) {
  256.       for( i=1;  i <= antGP; i++ )
  257.         gp += ( rand() % ternGP );
  258.     }
  259.     if ( (id == 'G') || (id == 'H') )  gp *= (multi * 10);
  260.     else                               gp *= multi;
  261.   }
  262.  
  263.   if ( PrPP != 0 ) {
  264.     tall = rand() % 100;
  265.     if ( tall < PrPP ) {
  266.       for( i=1;  i <= antPP; i++ )
  267.         pp += ( rand() % ternPP );
  268.     }
  269.     pp *= multi;
  270.   }
  271.  
  272.   if ( Prgem != 0 ) {
  273.     tall = rand() % 100;
  274.     if ( tall < Prgem ) {
  275.       for( i=1;  i <= antTG; i++ )
  276.         gem += ( rand() % ternG );
  277.     }
  278.   }
  279.  
  280.   if ( PrJew != 0 ) {
  281.     tall = rand() % 100;
  282.     if ( tall < PrJew ) {
  283.       for( i=1;  i <= antTJ; i++ )
  284.         jew += ( rand() % ternJ );
  285.     }
  286.   }
  287.  
  288.   if ( PrSpe != 0 ) {
  289.     tall = rand() % 100;
  290.     if ( tall < PrSpe ) {
  291.       for( i=1;  i <= antTS; i++ )
  292.         spe += ( rand() % ternS );
  293.     }
  294.   }
  295.  
  296.   if ( PrMag != 0 ) {
  297.     tall = rand() % 100;
  298.     if ( tall < PrMag )  mag = 1;
  299.   }
  300.  
  301.   /************** Utskrift ************/
  302.   fprintf( f, "Overview over generated treasure\n\n");
  303.   sprintf( txt, "Overview over generated treasure\n\n");
  304.   if ( cp != 0 ) {
  305.     fprintf( f, "%u cp\n", cp );
  306.     sprintf( str, "%u cp\n", cp );
  307.     strcat( txt, str );
  308.   }
  309.   if ( sp != 0 ) {
  310.     fprintf( f, "%u sp\n", sp );
  311.     sprintf( str, "%u sp\n", sp );
  312.     strcat( txt, str );
  313.   }
  314.   if ( ep != 0 ) {
  315.     fprintf( f, "%u ep\n", ep );
  316.     sprintf( str, "%u ep\n", ep );
  317.     strcat( txt, str );
  318.   }
  319.   if ( gp != 0 ) {
  320.     fprintf( f, "%u gp\n ", gp );
  321.     sprintf( str, "%u gp\n", gp );
  322.     strcat( txt, str );
  323.   }
  324.   if ( pp != 0 ) {
  325.     fprintf( f, "%u pp\n", pp );
  326.     sprintf( str, "%u pp\n", pp );
  327.     strcat( txt, str );
  328.   }
  329.   if ( gem != 0 ) {
  330.     fprintf( f, "%u number of gem(s)\n", gem );
  331.     sprintf( str, "%u number of gem(s)\n", gem );
  332.     strcat( txt, str );
  333.   }
  334.   if ( jew != 0 ) {
  335.     fprintf( f, "%u piece(s) of Jewelry\n", gem );
  336.     sprintf( str, "%u piece(s) of Jewelry\n", gem );
  337.     strcat( txt, str );
  338.   }
  339.   if ( spe != 0 ) {
  340.     fprintf( f, "%u number of special treasure's\n", spe );
  341.     sprintf( str, "%u number of special treasure's\n", spe );
  342.     strcat( txt, str );
  343.   }
  344.   if ( mag == 1 ) {
  345.     fprintf( f, "Magical Items : %s\n", strM );
  346.     sprintf( str, "Magical Items : %s\n", strM );
  347.     strcat( txt, str );
  348.   }
  349.  
  350.   xp = (cp/1000) + (sp/100) + (ep/2) + gp + (5*pp);
  351.  
  352.   fprintf( f, "\nTreasure give %1.0f in xp points\n", xp );
  353.   sprintf( str, "\nTreasure give %1.0f in xp points\n", xp );
  354.   strcat( txt, str );
  355.   strcat( txt, "\n<<  Press any key to pop down this window  >>\n" );
  356.   Treasure( txt );
  357. }
  358.  
  359. /***********************************************************************/
  360. /*
  361.     Tre is called from Treasure_Gen, and valg determines which kind of
  362.   treasure to be generated. If you want to change the treasure it is easy
  363.   to do it here.
  364. */ 
  365. int Tre( valg,f )
  366. char *valg;
  367. FILE *f;
  368.  
  369. {
  370.    switch ( valg[0] ) {
  371.  
  372.      case 'A' : T_En( 25,1,6, 30,1,6,  20,1,4, 35,2,6, 25,1,2,
  373.                       50,6,6,  50,6,6,  10,1,2, 30,"Any 3",  1000, f, 'A' );
  374.                 break;
  375.      case 'B' : T_En( 50,1,8, 25,1,6,  25,1,4, 35,1,3, 0,0,0,
  376.                       25,1,6,  25,1,6,  0,0,0, 10,"1 Sword and misc. weapon",  
  377.                       1000, f, 'B' );
  378.                 break;
  379.      case 'C' : T_En( 20,1,12, 30,1,4,  10,1,4, 0,0,0, 0,0,0,
  380.                       25,1,4,  25,1,4,  5,1,2, 10,"Any 2",  1000, f, 'C' );
  381.                 break;
  382.      case 'D' : T_En( 10,1,8,  15,1,12, 0,0,0,  60,1,6, 0,0,0,
  383.                       30,1,8,  30,1,8, 10,1,2, 15,"Any 2 + 1 potion",  
  384.                       1000, f, 'D' );
  385.                 break;
  386.      case 'E' : T_En( 5,1,10, 30,1,12, 25,1,4, 25,1,8, 0,0,0,
  387.                       10,1,10,  10,1,10,  15,1,2, 25,"Any 3 + 1 scroll",  
  388.                       1000, f, 'E' );
  389.                 break;
  390.      case 'F' : T_En( 0,0,0,  10,2,10, 20,1,8, 45,1,8, 30,1,3,
  391.                  20,2,12, 10,1,12, 20,1,3, 30,"1 Potion, 1 scroll 3 weapons",
  392.                       1000, f, 'F' );
  393.                 break;
  394.      case 'G' : T_En( 0,0,0,  0,0,0,  0,0,0,  50,1,4,  50,1,4,
  395.                       25,3,6,  25,1,10,  30,1,3, 35,"Any 4 + 1 scroll", 
  396.                       1000, f, 'G' );
  397.                 break;
  398.      case 'H' : T_En( 25,3,8, 50,1,100, 50,1,4, 50,1,6, 25,5,4,
  399.                  50,1,100, 50,1,4, 10,1,2, 15,"Any 4 + one potion and scroll", 
  400.                       1000, f, 'H' );
  401.                 break;
  402.      case 'I' : T_En( 0,0,0, 0,0,0,  0,0,0, 0,0,0, 0,0,0,
  403.                       50,2,6, 50,2,6, 5,1,2, 15,"Any 1", 1, f, 'I' );
  404.                 break;
  405.      case 'J' : T_En( 25,1,4, 10,1,3,  0,0,0, 0,0,0, 0,0,0,
  406.                       0,0,0,  0,0,0,  0,0,0, 0,"ingen",1000, f, 'J' );
  407.                 break;
  408.      case 'K' : T_En( 0,0,0, 30,1,6,  10,1,2, 0,0,0, 0,0,0,
  409.                       0,0,0,  0,0,0,  0,0,0, 0,"ingen",1000, f, 'K' );
  410.                 break;
  411.      case 'L' : T_En( 0,0,0, 0,0,0,  0,0,0, 0,0,0, 0,0,0,
  412.                       50,1,4,  0,0,0,  0,0,0, 0,"ingen",  1, f, 'L' );
  413.                 break;
  414.      case 'M' : T_En( 0,0,0, 0,0,0,  0,0,0, 40,2,4, 50,3,10,
  415.                       55,5,4,  45,2,6,  0,0,0, 0,"ingen",
  416.                       1000, f, 'M' );
  417.                 break;
  418.      case 'N' : T_En( 0,0,0, 0,0,0,  0,0,0, 0,0,0, 0,0,0,
  419.                       0,0,0,0,0,0, 10,1,2, 40,"2-8 potions", 1, f,'N');
  420.                 break;
  421.      case 'O' : T_En( 0,0,0, 0,0,0,  0,0,0, 0,0,0, 0,0,0,
  422.                       0,0,0,0,0,0,10,1,3, 50,"1-4 scrolls", 1,f,'O');
  423.                 break;
  424.  
  425.      /******* Treasure for individual ********/
  426.  
  427.      case 'P' : T_En( 100,3,8, 0,0,0,  0,0,0, 0,0,0, 0,0,0,
  428.                       0,0,0,  0,0,0,  0,0,0, 0,"ingen",  1, f, 'P' );
  429.                 break;
  430.      case 'Q' : T_En( 0,0,0, 100,3,6,   0,0,0, 0,0,0, 0,0,0,
  431.                       0,0,0,  0,0,0,  0,0,0, 0,"ingen",  1, f, 'Q' );
  432.                 break;
  433.      case 'R' : T_En( 0,0,0, 0,0,0,  100,2,6, 0,0,0, 0,0,0,
  434.                       5,1,2,  0,0,0,  0,0,0, 0,"ingen",  1, f, 'R' );
  435.                 break;
  436.      case 'S' : T_En( 0,0,0, 0,0,0,  0,0,0, 100,2,4, 0,0,0,
  437.                       5,1,2,  0,0,0,  0,0,0, 0,"ingen",  1, f, 'S' );
  438.                 break;
  439.      case 'T' : T_En( 0,0,0, 0,0,0,  0,0,0, 0,0,0, 100,1,6,
  440.                       5,1,2,  0,0,0,  0,0,0, 0,"ingen",  1, f, 'T' );
  441.                 break;
  442.      case 'U' : T_En( 10,1,100, 10,1,100,  0,0,0, 5,1,100, 0,0,0,
  443.                       5,1,2,  10,1,4,  5,1,2, 2,"Any 1",  1, f, 'U' );
  444.                 break;
  445.      case 'V' : T_En( 0,0,0,  10,1,100,  5,1,100, 10,1,100, 5,1,100,
  446.                       10,1,2, 10,1,4,    5,1,2,   5,"Any 1",  1,f, 'V');
  447.    }
  448. }
  449.  
  450. /***********************************************************************/
  451. /*
  452.     Remove the treasure window, when we are pressing a button in the treasure
  453.   window.
  454. */
  455.  
  456. static void PopDown( w, xe, str, num  )
  457. Widget    w;
  458. XEvent    *xe;
  459. String    *str;
  460. Cardinal  *num;
  461.  
  462. {
  463.   XtDestroyWidget( XtParent(w) );
  464. }
  465.  
  466. /***********************************************************************/
  467. /*
  468.    Quik and probaly a dirty way out.
  469. */
  470. static void quit()
  471. {
  472.   exit(0);
  473. }
  474.  
  475.